package com.example.springmvc.controller;

import com.example.springmvc.entity.Userinfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UserController {

    /**
     * 获取 header 中的某个信息
     */
    @RequestMapping("/gethd")
    public Object getHeader(@RequestHeader("User-Agent") String ua) {
        return "head -> " + ua;
    }

    /**
     * 获取cookie
     */
    @RequestMapping("/cookie")
    public Object getCookie(@CookieValue("bite") String bite) {
        return "cookie：" + bite;
    }

    private static final String SESSION_KEY = "USERINFO_SESSION_KEY";
    /**
     * 存储 Session
     */
    @RequestMapping("/setsess")
    //@PostConstruct
    public void setSess(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute(SESSION_KEY,"zhangsan");

    }
    /**
     * 如何获取session
     */
    @RequestMapping("/getsession")
    public Object getSession(@SessionAttribute(SESSION_KEY) String name) {
        return "session ->" + name;
    }

    /**
     * 获取对象
     * @param userinfo
     * @return
     */
    @RequestMapping("/reg")
    public Object reg(Userinfo userinfo) {
        System.out.println(userinfo);
        return userinfo;
    }


    @RequestMapping("/myupload")
    public Object upload(@RequestPart("myimg") MultipartFile file) {
        String fileName = UUID.randomUUID() +  //文件名
                file.getOriginalFilename().substring(  // 文件后缀
                        file.getOriginalFilename().lastIndexOf("."));
        File saveFile = new File("E:\\img" + fileName);
        try {
            file.transferTo(saveFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    @RequestMapping("/reg4/{name}/{password}")
    public Object reg4(@PathVariable String name,@PathVariable("password") String passward) {
        return "name->" + name + "| password->" + passward;
    }

    @RequestMapping("/reg3")
    public Object reg3(@RequestBody Userinfo userinfo) {
        return userinfo;
    }
    @RequestMapping("/reg2")
    public Object reg2(@RequestParam(value = "username",required = false) String name,String password) {
        System.out.println("reg2");
        return name + " : " + password;
    }

    //@RequestMapping(value = "/sayHi",method = RequestMethod.POST) //可以是一级路由 也可以是n级路由
    @RequestMapping("/sayHi")
    // 既支持get请求 又支持 post 请求
    // method = RequestMethod.POST 指定POST请求
    public String sayHi(String name,String password) {
        return "hi spring mvc" + name + "| password : " + password;
    }

    @RequestMapping("sayHi2")
    public String sayHi2(int id) { //参数传递不能用基础数据类型->因为基础数据会报错
        return "hi" + id; //报500错误
    }

    @RequestMapping("sayHi3")
    public String sayHi3(Integer id) {
        return "hi" + id;
    }

    @RequestMapping("sayHi4")
    public String sayHi4(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("http://www.javacn.site");
        return "hi" + request.getParameter("name");
    }

    @PostMapping("sayhello") //只支持post请求
    public String sayHello() {
        return "hello springmvc";
    }

    @GetMapping("sayla")
    public String satla() {
        return "la sprignmvc";
    }
}
